/*
 * Copyright (c) sl All rights reserved.
 * 本软件源代码版权归sl所有,未经许可不得任意复制与传播.
 */
package com.sl.crawler.web.controller;

import com.sl.crawler.common.utils.DateUtils;
import com.sl.crawler.domain.House;
import com.sl.crawler.domain.common.Page;
import com.sl.crawler.robot.Crawler;
import com.sl.crawler.service.HouseService;
import com.sl.crawler.service.base.BaseService;
import com.sl.crawler.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;

/**
 * @author sl
 */
@Controller
@RequestMapping(value = "/house")
public class HouseController extends BaseController {
    private static final Logger LOG = LoggerFactory.getLogger(HouseController.class);
    @Autowired
    private HouseService houseService;

    /**
     * 获取Service操作类
     */
    @Override
    public BaseService<House, Long> getService() {
        return houseService;
    }

    /**
     * 获取视图路径
     */
    @Override
    public String getModelName() {
        return "house";
    }

    /**
     * 列表展示
     *
     * @param house 实体对象
     * @param page  分页对象
     * @return
     */
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    public String list(House house, Page<House> page, Model view) throws Exception {
        return baseList(house, page, view);
    }

    /**
     * 响应新增(修改)页面
     *
     * @param id 对象编号
     * @return
     */
    @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable Long id, Model view) throws Exception {
        return baseEdit(id, view);
    }

    /**
     * 通过编号删除对象
     *
     * @param id 对象编号
     * @return
     */
    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable Long id, Model view) throws Exception {
        return baseDelete(id, view);
    }

    /**
     * 通过编号查看对象
     *
     * @param id 对象编号
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String view(@PathVariable Long id, Model view) throws Exception {
        return baseView(id, view);
    }

    /**
     * 保存方法
     *
     * @param house 实体对象
     * @return
     */
    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.GET})
    public String save(House house, Model view) throws Exception {
        return baseSave(house);
    }


    /**
     * 导出CSV
     *
     * @param house
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "exportCsv")
    public void exportCsv(@ModelAttribute("house") House house, HttpServletResponse response) throws Exception {
        baseExportCsv(house, new Page<House>(1, 5000), response);
    }

    /**
     * 获取导出csv的表头
     *
     * @return
     */
    @Override
    public String getExportCsvTitle() {
        String title = "主键,小区,总价,单价,关注人数,带看人数,发布时间,位置详情,房源标签,一级地区,二级地区,房屋户型,建筑面积,房子朝向,装修情况,配备电梯,所在楼层,总楼层,修建年代,建筑类型,标题,房屋编号,是否有效,备注,修改人,创建时间,修改时间,";
        return title;
    }

    /**
     * 获取导出csv的数据明细
     *
     * @param o
     * @return
     */
    @Override
    public String getExportCsvRow(Object o) {
        House house = (House) o;
        StringBuilder sb = new StringBuilder();
        sb.append("\"").append(house.getId()).append("\"").append(",");
        sb.append(house.getXiaoqu()).append(",");
        sb.append("\"").append(house.getTotal()).append("\"").append(",");
        sb.append("\"").append(house.getUnitPrice()).append("\"").append(",");
        sb.append("\"").append(house.getFollowCount()).append("\"").append(",");
        sb.append("\"").append(house.getLookCount()).append("\"").append(",");
        sb.append(house.getPublishTime()).append(",");
        sb.append(house.getPositionInfo()).append(",");
        sb.append(house.getHouseTag()).append(",");
        sb.append(house.getLocationOne()).append(",");
        sb.append(house.getLocationTwo()).append(",");
        sb.append(house.getHouseType()).append(",");
        sb.append("\"").append(house.getHouseArea()).append("\"").append(",");
        sb.append(house.getHouseFacing()).append(",");
        sb.append(house.getDecorationSituation()).append(",");
        sb.append(house.getHaveElevator()).append(",");
        sb.append(house.getHouseFloor()).append(",");
        sb.append("\"").append(house.getTotalFloor()).append("\"").append(",");
        sb.append("\"").append(house.getBuildingYears()).append("\"").append(",");
        sb.append(house.getBuildingType()).append(",");
        sb.append(house.getTitle()).append(",");
        sb.append(house.getHouseId()).append(",");
        sb.append("\"").append(house.getYn()).append("\"").append(",");
        sb.append(house.getRemark()).append(",");
        sb.append(house.getModifiedUser()).append(",");
        sb.append(DateUtils.format(house.getCreatedDate())).append(",");
        sb.append(DateUtils.format(house.getModifiedDate())).append(",");
        sb.append("\r\n");
        return sb.toString();
    }
    //自定义扩展


    /**
     * 导出CSV
     *
     * @param house
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/lianJiaHouseCrawler", method = {RequestMethod.POST, RequestMethod.GET})
    public String lianJiaHouseCrawler(House house, Model view) throws Exception {
        try {
            Crawler.crawlerSingle("crawler/lianjia/house.yaml");
        } catch (Exception e) {
            LOG.error("执行错误", e);
        }
        return "redirect:/" + getModelName();
    }

}